package com.ytjj.qmyx.mall.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TranslateTimeUtil {

    /**
     * 相差时间，转换成多少天，多少小时，多少分的格式
     * @param start
     * @param end
     * @return
     */
    public static String translateTime(Date start, Date end) {
        if (start == null || end == null) {
            return "";
        }
        long time = end.getTime() - start.getTime();
        long days = time / (1000 * 60 * 60 * 24);
        long hours = (time - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (time - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (time - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        String descript = "";
        if (days > 0) {
            descript = descript + (days + "天");
        }
        if (hours > 0) {
            descript = descript + (hours + "小时");
        }
        if (minutes > 0) {
            descript = descript + (minutes + "分");
        }
        if (seconds > 0) {
            descript = descript + (seconds + "秒");
        }
        return descript;
    }

    /**
     * string类型转换为date类型
     * @param dateStr string类型的时间
     * @param pattern 转换的时间模式  yyyy-MM-dd HH:mm:ss
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String dateStr, String pattern) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = simpleDateFormat.parse(dateStr);
        return date;
    }
}